package org.whuims.web.document;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.whuims.web.util.DocumentExtractor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;

@Controller
@RequestMapping(value = "/documents")
public class DocumentFileController {

    @Autowired
    private DocumentExtractor documentExtractor;

    @RequestMapping(value = "uploadBasicHead", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String ajaxUpload(HttpServletRequest request) throws IllegalStateException,
            IOException {
        MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String fileName = "";
        String uploadPath = "resource/docs/";
        String path = request.getSession().getServletContext().getRealPath("/") + uploadPath;
        File uploadPathFile = new File(path);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdirs();
        }
        String realPath = "";
        for (Iterator it = multipartRequest.getFileNames(); it.hasNext(); ) {
            String key = (String) it.next();
            MultipartFile mulfile = multipartRequest.getFile(key);
            fileName = mulfile.getOriginalFilename();
//            fileName = handlerFileName(fileName);
            File file = new File(path + fileName);
            System.out.println(file.getAbsolutePath());
            mulfile.transferTo(file);
        }
        realPath = "{\"imagePath\":\"" + fileName + "\"}";
        return realPath;
    }

    //预览，获取图片流
    @RequestMapping(value = "readImage", produces = "text/plain;charset=UTF-8")
    public void readImage(HttpServletRequest request, HttpServletResponse response) {
        String imagePath = request.getSession().getServletContext().getRealPath("/") + request.getParameter("imagePath");
        try {
            File file = new File(imagePath);
            System.out.println(file.length());
            if (file.exists()) {
                DataOutputStream temps = new DataOutputStream(response
                        .getOutputStream());
                DataInputStream in = new DataInputStream(
                        new FileInputStream(imagePath));
                byte[] b = new byte[2048];
                while ((in.read(b)) != -1) {
                    temps.write(b);
                    temps.flush();
                }
                in.close();
                temps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "readContent")
    public void readContent(HttpServletRequest request, HttpServletResponse response, Model model) throws IllegalStateException,
            IOException {
        String fileName = request.getParameter("fileName");
        String uploadPath = "resource/docs/";
        String path = request.getSession().getServletContext().getRealPath("/") + uploadPath;
        File uploadPathFile = new File(path + fileName);
        String content = documentExtractor.extract(uploadPathFile);
        String contentPure = "{\"contentPure\":\"" + content + "\"}";
        OutputStream out = response.getOutputStream();
        try {
            out.write(content.getBytes());
            out.flush();
            out.close();
        } catch (Exception e) {
            System.err.println("出错了");
        }
    }
}
